package com.hainatong.shiphelper;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModelProvider;
import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.mvvmbase.utils.ClassUtil;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract  class MvvmLazyFragment<V extends ViewDataBinding, VM extends AndroidViewModel> extends Fragment
{

    private Context mContext;
    private boolean isLoaded = false;

    // ViewModel
    protected VM viewModel;

    // 布局view
    protected V viewDataBinding;

    protected String TAG = this.getClass().getSimpleName();

    //RxBus  取消订阅 关系时  使用
    private CompositeDisposable mCompositeDisposable;

    /**
     * Fragment生命周期 onAttach -> onCreate -> onCreatedView -> onActivityCreated
     * -> onStart -> onResume -> onPause -> onStop -> onDestroyView -> onDestroy
     * -> onDetach 对于 ViewPager + Fragment 的实现我们需要关注的几个生命周期有： onCreatedView +
     * onActivityCreated + onResume + onPause + onDestroyView
     */

    protected View rootView = null;
    /**
     * 布局是否创建完成
     */
    protected boolean isViewCreated = false;
    //没起 作用
    private LoadingDialog mLoading;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
        initParameters();
        mContext = getActivity();
    }


    /**
     * 初始化参数
     */
    protected void initParameters()
    {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        if (null == rootView)
        {
            viewDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
            rootView = viewDataBinding.getRoot();
        }
        initView(rootView);
        mLoading = new LoadingDialog( getActivity(), getString( R.string.loading ) );
        isViewCreated = true;
        Log.d(TAG, " : onCreateView");
        return rootView;
    }


    @Override
    public void onViewCreated(@NonNull View view,
                              @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        initViewModel();
        if (null != viewModel && getBindingVariable() > 0)
        {
            viewDataBinding.setVariable(getBindingVariable(), viewModel);
            viewDataBinding.executePendingBindings();
        }
        Log.d(TAG, " : onViewCreated");
    }

    /**
     * 初始化ViewModel
     */
    private void initViewModel()
    {
        Class<VM> viewModelClass = ClassUtil.getViewModel(this);
        if (viewModelClass != null)
        {
            viewModel = new ViewModelProvider(this).get(viewModelClass);
        }
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        isLoaded = false;
        isViewCreated = false;
        Log.d(TAG, " : onDestroyView");
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (!isLoaded && !isHidden())
        {
            // 将数据加载逻辑放到onResume()方法中
            Log.d("OABrokenApplyFragment", "lazyLoad()");
            lazyLoad();
            isLoaded = true;
        }

    }

    /**
     * 设置布局资源id
     *
     * @return
     */
    /**
     * 布局
     */
    @LayoutRes
    public abstract int getLayoutId();


    protected void initView(View view)
    {

    }

    /**
     * 初始化数据
     */
    protected void lazyLoad()
    {
    }


    /**
     * 获取参数
     */
    public abstract int getBindingVariable();


    /**
     * 失败重试,加载事件
     */
    protected abstract void onRetryBtnClick();

    public void showLoading()
    {
        mLoading.show();
    }

    public void hideLoading()
    {
        mLoading.hide();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (null != viewModel )
        {
            //((BaseViewModel)viewModel).onCleared();
        }
        if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed())
        {
            this.mCompositeDisposable.clear();
        }
        mLoading.dismiss();
        Log.d(TAG, " : onDestroy");
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        if (null != viewModel )
        {
            //viewModel.onCleared();
        }
        Log.d(TAG, " : onDetach");

    }

    public void addSubscription(Disposable disposable)
    {
        if (this.mCompositeDisposable == null)
        {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(disposable);
    }

    public void removeDisposable()
    {
        if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed())
        {
            this.mCompositeDisposable.dispose();
        }
    }
}
